package han.chensing.latos.common.level.content.objects;

import han.chensing.latos.common.level.content.LevelData;
import han.chensing.latos.common.level.content.timeLine.TimeLineEventSet;
import han.chensing.latos.common.level.content.timeLine.TimeLineRow;
import han.chensing.latos.common.level.content.timeLine.events.TimeLineEvent;
import han.chensing.latos.common.level.content.timeLine.frame.EditorEventTypes;
import han.chensing.latos.common.level.content.timeLine.frame.FrameEditor;

public class CommonBPM extends CommonObject{

    public static final String ROW_NAME_BPM="BPM";

    public CommonBPM(float offset) {
        super(ROW_NAME_BPM);
        TimeLineRow bpmRow=this.timeLineRow;
        bpmRow.setName(ROW_NAME_BPM);
        TimeLineEventSet<TimeLineEvent<FrameEditor>,FrameEditor> bpmEventSet = new TimeLineEventSet<>(EditorEventTypes.Bpm);
        FrameEditor e = new FrameEditor();
        e.setBpm(120);
        e.setTime(offset);
        e.addTypes(EditorEventTypes.Bpm);
        bpmEventSet.getKeyFrames().add(e);
        //bpmEventSet.getValueBook().initContainer(bpmEventSet);
        bpmRow.getTimeLineEventSets().add(bpmEventSet);
    }

    public static CommonBPM getObject(LevelData levelData){
        CommonObject object = levelData.findReservedObject(commonObject -> commonObject.getName().equals(ROW_NAME_BPM));
        if (object==null)return null;
        if (object instanceof CommonBPM commonBPM)return commonBPM;
        return null;
    }

    @SuppressWarnings("unchecked")
    public static TimeLineEventSet<TimeLineEvent<FrameEditor>,FrameEditor> getEventSet(LevelData levelData){
        CommonBPM object = getObject(levelData);
        assert object != null;
        TimeLineEventSet<?,?> timeLineEventSet = object.getTimeLineRow().getTimeLineEventSets().get(0);
        return (TimeLineEventSet<TimeLineEvent<FrameEditor>,FrameEditor>) timeLineEventSet;
    }
}
